package com.zp.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.system.api.entity.SysBaseDictData;
import com.zp.system.entity.bo.SysBaseDictDataBo;
import com.zp.system.entity.vo.SysBaseDictDataVo;
import com.zp.system.mapper.SysBaseDictDataMapper;
import com.zp.system.service.ISysBaseDictDataService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 系统平台字典Service业务层处理
 *
 * @author zhang peng
 * @date 2023-01-03
 */
@RequiredArgsConstructor
@Service
public class SysBaseDictDataServiceImpl implements ISysBaseDictDataService {

    private final SysBaseDictDataMapper baseMapper;


    /**
     * 查询系统平台字典
     */
    @Override
    public SysBaseDictDataVo queryById (String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询系统平台字典列表(分页)
     */
    @Override
    public TableDataInfo<SysBaseDictDataVo> queryPageList (SysBaseDictDataBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysBaseDictData> lqw = buildQueryWrapper(bo);
        Page<SysBaseDictDataVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询系统平台字典列表(不分页)
     */
    @Override
    public List<SysBaseDictDataVo> queryList (SysBaseDictDataBo bo) {
        LambdaQueryWrapper<SysBaseDictData> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysBaseDictData> buildQueryWrapper (SysBaseDictDataBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysBaseDictData> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getLabel()), SysBaseDictData::getLabel, bo.getLabel());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), SysBaseDictData::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getValue()), SysBaseDictData::getValue, bo.getValue());
        lqw.eq(bo.getSort() != null, SysBaseDictData::getSort, bo.getSort());
        lqw.eq(StringUtils.isNotBlank(bo.getOutpOrInp()), SysBaseDictData::getOutpOrInp, bo.getOutpOrInp());
        lqw.eq(bo.getCreateTime() != null, SysBaseDictData::getCreateTime, bo.getCreateTime());
        lqw.eq(bo.getUpdateTime() != null, SysBaseDictData::getUpdateTime, bo.getUpdateTime());
        lqw.orderByAsc(SysBaseDictData::getSort);
        return lqw;
    }

    /**
     * 新增系统平台字典
     */
    @Override
    public Boolean insertByBo (SysBaseDictDataBo bo) {
        SysBaseDictData add = BeanUtil.toBean(bo, SysBaseDictData.class);
        add.setDelFlag("0");
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改系统平台字典
     */
    @Override
    public Boolean updateByBo (SysBaseDictDataBo bo) {
        SysBaseDictData update = BeanUtil.toBean(bo, SysBaseDictData.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (SysBaseDictData entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除系统平台字典
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
